/*
 * 
 * Created on Jul 27, 2005
 */
package com.wgo.precise.client.ui.view.editor.requirementtable;

import java.util.List;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import com.wgo.precise.client.ui.controller.exceptions.RematoClientException;
import com.wgo.precise.client.ui.model.AbstractPropertySpesification;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.EnumPropertySpecification;
import com.wgo.precise.client.ui.model.PropertyDefWrapper;
import com.wgo.precise.client.ui.model.PropertyValue;
import com.wgo.precise.client.ui.model.RequirementWrapper;
import com.wgo.precise.client.ui.model.facade.ILabelProviderItem;
import com.wgo.precise.client.ui.model.facade.IPropertiesContainer;


/**
 * 
 * @author Petter L. H. Eide
 * @version $Id: TableLabelProvider.java,v 1.1 2006-01-23 19:58:37 petterei Exp $
 */
public class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
    
    private List<AbstractPropertySpesification> spesifications;

    public TableLabelProvider(List<AbstractPropertySpesification> spesifications) {
        super();
        this.spesifications = spesifications;
    }

    /**
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
     */
    public Image getColumnImage(Object element, int columnIndex) {
    	Image result = null;
    	if ( element instanceof ClientModelItem ) {
            AbstractPropertySpesification spec = spesifications.get(columnIndex);
            PropertyValue propertyValue = ((IPropertiesContainer) element).getCurrentPropertyValue(spec);
            Object value = null;
            if (propertyValue != null) {
                value = propertyValue.getValue();
            } else {
                throw new RematoClientException(getClass().getSimpleName() + ": Did not find value for propertyId: " + spec.getIdentifier() ) ;
            }
            if (value instanceof ILabelProviderItem ) {
            	result = ((ILabelProviderItem)value).getImage();
            }
        }
        return result;
    }

    /**
     * TODO: refactor to better architecture.
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
     */
    @SuppressWarnings("unchecked")
	public String getColumnText(Object element, int columnIndex) {
        String result = "";
        if ( element instanceof ClientModelItem ) {
            AbstractPropertySpesification spec = spesifications.get(columnIndex);
            PropertyValue propertyValue = ((IPropertiesContainer) element).getCurrentPropertyValue(spec);
            if ( null == propertyValue && element instanceof RequirementWrapper && spec instanceof PropertyDefWrapper){
            	((RequirementWrapper)element).verifyProperty((PropertyDefWrapper)spec);
            	propertyValue = ((IPropertiesContainer) element).getCurrentPropertyValue(spec);
            }
            Object value = null;
            if (propertyValue != null) {
                value = propertyValue.getValue();
            } else {
                throw new RematoClientException(getClass().getSimpleName() + ": Did not find value for propertyId: " + spec.getIdentifier() ) ;
            }
            if (value instanceof ILabelProviderItem ) {
            	result = ((ILabelProviderItem)value).getText();
            } else if (spec instanceof EnumPropertySpecification){
            	EnumPropertySpecification enumSpec = (EnumPropertySpecification) spec;
            	result = enumSpec.getTextLabel((Integer)value);
            } else{
            	result = spec.getPropertyType().createStringValue(value);
            }
        } else if (element instanceof ILabelProviderItem) {
            ILabelProviderItem item = (ILabelProviderItem) element;
            result = item.getText();
        } else {
            result =  getText(element);
        }
        return result;
    }
}
